import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

public class JedisDemo1 {
    Jedis jedis=new Jedis("192.168.80.7",6379);
    //测试连接
    @Test
    public void test() throws Exception {
        String ping = jedis.ping();
        if("PONG".equals(ping))
            System.out.println("连接成功");
    }
    //操作key 操作字符串
    @Test
    public void test1() throws Exception {
        Set<String> keys = jedis.keys("*");
        //System.out.println(keys);
        for (String key:keys
             ) {
            System.out.println(key);
        }
        String set = jedis.set("k1", "v1");
        String k11 = jedis.get("k1");
        System.out.println(k11);
        Long k1 = jedis.ttl("k1");//查看生存时间
        Boolean k12 = jedis.exists("k2");
        Long k13 = jedis.expire("k1", 20);//设置过期时间
        String mset = jedis.mset("k1", "wxc", "age", "21");
        List<String> mget = jedis.mget("name", "k1", "age");
    }

    //操作list
    @Test
    public void test2() throws Exception {
        jedis.lpush("key1","wxc","lucy");
        List<String> key1 = jedis.lrange("key1", 0, -1);
        System.out.println(key1);
    }
    //操作set
    @Test
    public void test3() throws Exception {
        jedis.sadd("names","wxc","jack");
        jedis.sadd("names","123");
        Set<String> name = jedis.smembers("names");
        System.out.println(name);
        //srem删除
    }
    //操作hash
    @Test
    public void test4() throws Exception {
        jedis.hset("users","age","20");
        String hget = jedis.hget("users", "age");
        System.out.println(hget);
    }
    //操作zset
    @Test
    public void test5() throws Exception {
        jedis.zadd("china", 100d, "shanghai");
        Set<String> china = jedis.zrange("china", 0, -1);
        System.out.println(china);
    }
}
